package com.bigzero.workflow.modules.system.controller;

import com.alibaba.fastjson2.JSONObject;
import com.bigzero.workflow.common.exception.WorkflowException;
import com.bigzero.workflow.common.model.R;
import com.bigzero.workflow.modules.system.dto.LoginDto;
import com.bigzero.workflow.modules.system.service.UserService;
import com.bigzero.workflow.modules.system.vo.UserVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录
 *
 * @author fanxinxiong
 * @date 2025-02-13 11:03
 **/
@RestController
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param dto 参数
     */
    @Tag(name = "系统管理-登录")
    @Operation(summary = "登录", description = "登录")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功响应",
            content = @Content(
               mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = UserVo.class))
            ))
    })
    @PostMapping("login")
    public R<JSONObject> login(@RequestBody LoginDto dto){
        if(StringUtils.isBlank(dto.getUserName())){
            throw new WorkflowException("用户不能为空!");
        }
        if(StringUtils.isBlank(dto.getPassword())){
            throw new WorkflowException("密码不能为空!");
        }
        JSONObject respJson = userService.login(dto);
        return R.ok(respJson);
    }

    @Tag(name = "系统管理-knife4j登录")
    @ApiOperationSupport(author = "范新雄", order = 3)
    @Operation(summary = "knife4j登录", description = "knife4j登录")
    @PostMapping("/knife4j/login")
    public R<JSONObject> knife4jLogin(@RequestBody LoginDto dto){
        JSONObject respJson = new JSONObject();
        try{
            if(StringUtils.isBlank(dto.getUserName())){
                throw new WorkflowException("用户不能为空!");
            }
            if(StringUtils.isBlank(dto.getPassword())){
                throw new WorkflowException("密码不能为空!");
            }
            respJson = userService.login(dto);
            StringBuilder sb = new StringBuilder();
            sb.append(" var code=ke.response.data.code; ");
            sb.append(" if(code===200){ ");
            sb.append("    var token=ke.response.data.data.token; ");
            sb.append("    ke.global.setAllHeader('Token',token); ");
            sb.append("    ke.global.setAllParameter('Token',token); ");
            sb.append(" }");
            respJson.put("afterScript", sb.toString());
        }catch(Exception e){
            e.printStackTrace();
        }
        return R.ok(respJson);
    }
}
